
package com.xbongbong.pro.statistic.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.dao.ChartDao;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.sys.help.SysEnvHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
* @author autoGen
* @time 2019-01-07 11:13:40
*/
@Service("chartModel")
public class ChartModelImpl implements ChartModel {

	@Resource
	private ChartDao dao;
	@Resource
	private ChartCustomModel chartCustomModel;
	@Resource
	private SysEnvHelp sysEnvHelp;

	@Override
	public Integer insert(ChartEntity entity){
		long now = DateUtil.getInt();
		entity.setAddTime(now);
		entity.setUpdateTime(now);
		Integer insert = dao.insert(entity);
		//TODO  返回实体id
		return insert;
	}

	@Override
	public Integer update(ChartEntity entity){
		entity.setUpdateTime((long)DateUtil.getInt());
		//TODO  返回实体id
		return dao.update(entity);
	}

	@Override
	public Integer save(ChartEntity entity) {
		if(entity.getId() == null || entity.getId().equals(0L)) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<ChartEntity> entityList) {
    	dao.insertBatch(entityList);
    }
	@Override
	public Integer deleteByKey( Long key,  String corpid){
		return dao.deleteByKey(key , corpid);
	}
	@Override
	public ChartEntity getByKey( Long key,  String corpid){
		return dao.getByKey(key , corpid);
	}

	@Override
	public List<ChartEntity> findEntitys(Map<String ,Object>  param){
//		sysEnvHelp.addGrayEnvironmentQueryCondition4Default(param);
		return dao.findEntitys(param);
	}

	@Override
	public List<ChartEntity> findMutiEntitys(Map<String ,Object>  param){
		return dao.findMutiEntitys(param);
	}

    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public List<ChartEntity> getChartListByCategoryId(String corpid, Long categoryId){
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("categoryId", categoryId);
		return findEntitys(param);
	}

	@Override
	public boolean checkRepeat(String corpid, String name, Long setId, Integer chartType, Integer statisticsType) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("name", name);
		//chartType 已经在调用处根据statisticsType转换过了
		param.put("chartType", chartType);
		if (setId != null && setId > 0) {
			param.put("idNeq", setId);
		}
		Integer count;
		if(Objects.equals(StatisticsTypeEnum.SYSTEM_STATISTIC.getCode(),statisticsType)){
			//匹配系统图表
			count = getEntitysCount(param);
		}else{
			//匹配自定义图表
			count =  chartCustomModel.getEntitysCount(param);
		}
		return count > 0;
	}

	@Override
	public List<ChartEntity> getList(String corpid, Integer enable, Integer chartType) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("chartType", chartType);
		param.put("orderByStr", "sort desc, id asc");
		if (enable != null) {
			param.put("enable", enable);
		}
		return findEntitys(param);
	}

	@Override
	public List<ChartEntity> getList(String corpid, Integer enable, Integer chartType,String orderByStr) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("chartType", chartType);
		if (StringUtil.isNotEmpty(orderByStr)){
			param.put("orderByStr", "update_time desc,sort desc, id asc");
		}else {
			param.put("orderByStr", "sort desc, id asc");
		}
		if (enable != null) {
			param.put("enable", enable);
		}
		return findEntitys(param);
	}

	@Override
	public ChartEntity getSearch(String corpid, Long categoryId) {
		Integer chartType = ChartTypeEnum.MULTIPLE_SEARCH.getCode();
		return  dao.getSearch(corpid,categoryId,chartType);
	}

	@Override
	public ChartEntity getSystemChartByCode(String corpid, SystemCodeEnum systemCodeEnum) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("statisticsType", StatisticsTypeEnum.SYSTEM_STATISTIC.getCode());
		param.put("systemCode", systemCodeEnum.getSystemCode());
		List<ChartEntity> list = findEntitys(param);
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public ChartEntity getEntityByChartType(String corpid, ChartTypeEnum chartTypeEnum) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("chartType", chartTypeEnum.getCode());
		param.put("enable", BasicConstant.IS_USE);
		List<ChartEntity> list =  findEntitys(param);
		if (list == null || list.isEmpty()) {
		    return new ChartEntity();
		}
		return list.get(0);
	}

	@Override
	public List<ChartEntity> getStockChartList() {
		return dao.getStockChartList();
	}

	/**
	 * 批量更新enable
	 * @param param 查询参数map
	 * @author xingxing.xiao
	 */
	@Override
	public void batchUpdateEnable(Map<String, Object> param) {
		dao.batchUpdateEnable(param);
	}

	@Override
	public void batchUpdateNumFormat(String corpid, List<Long> idIn, Integer numFormat) {
		dao.batchUpdateNumFormat(corpid, idIn, numFormat);
	}

	@Override
	public List<ChartEntity> getReturnStockChartList() {
		return dao.getReturnStockChartList();
	}
	@Override
	public List<ChartEntity> getEntitysByNewData() {
		return dao.getEntitysByNewData();
	}

    @Override
    public void updateBatch(List<ChartEntity> chartEntityList, String corpid) {
        dao.updateBatch(chartEntityList, corpid);
    }

	@Override
	public void updateName(Long id, String corpid, String name, String oldName, String memo) {
		dao.updateName(id, corpid, name, oldName, memo);
	}

	@Override
	public void updateButtonAttr(Long id, String corpid, String buttonAttr) {
		dao.updateButtonAttr(id, corpid ,buttonAttr);
	}

	@Override
	public void updateBatchButtonAttr(List<ChartEntity> chartEntityList) {
		dao.updateBatchButtonAttr(chartEntityList);
	}

}

